package ld.servlet;

import com.alibaba.fastjson.JSON;
import ld.pojo.Emp;
import ld.vo.EmpVo;
import ld.utils.PageResult;
import ld.utils.R;
import ld.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Author: Liudao
 * @Date: 2022-08-04-14:48
 * @Description:
 */
@WebServlet("/emp")
public class EmpServlet extends HttpServlet {
    @Autowired
    private EmpService empService;
    @Override
    public void init()  {
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        empService = webApplicationContext.getBean(EmpService.class);

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        //得到请求参数
        String cmd = req.getParameter("cmd");
        //根据请求调用对应的方法
        if (!StringUtils.isEmpty(cmd)){
            if ("list".equals(cmd)){    //查询全部
                list(req,resp);
            }
            if ("findByPage".equals(cmd)){      //分页查找
                findByPage(req,resp);
            }
            if ("search".equals(cmd)){
                search(req,resp);
            }
            if ("add".equals(cmd)){
                add(req,resp);
            }
            if ("update".equals(cmd)){
                update(req,resp);
            }
            if ("delete".equals(cmd)){
                delete(req,resp);
            }
                    
        }
        
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String info = "";
        try {
            String[] eids = req.getParameter("eids").split(",");
            empService.delete(eids);
            R ok = R.ok();
            info = JSON.toJSONString(ok);
        }catch (Exception e){
            e.printStackTrace();
            R error = R.error();
            info = JSON.toJSONString(error);
        }
        resp.getWriter().println(info);
        resp.getWriter().flush();
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String info = "";
        try {
            String emp = req.getParameter("emp");
            emp = new String(emp.getBytes("ISO8859-1"),"UTF-8");
            Emp emp1 = JSON.parseObject(emp, Emp.class);
            empService.update(emp1);
            R ok = R.ok();
            info = JSON.toJSONString(ok);
        }catch (Exception e){
            e.printStackTrace();
            R error = R.error();
            info = JSON.toJSONString(error);
        }
        resp.getWriter().println(info);
        resp.getWriter().flush();
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String info = "";
        try {
            // 得到前端传入的数据
            String emp = req.getParameter("emp");
            //处理乱码
            emp = new String(emp.getBytes("ISO8859-1"), "UTF-8");
            //转换为员工对象
            Emp emp1 = JSON.parseObject(emp, Emp.class);
            //添加到数据库中
            empService.add(emp1);
            R ok = R.ok();
            info = JSON.toJSONString(ok);
        }catch (Exception e){
            e.printStackTrace();
            R error = R.error();
            info = JSON.toJSONString(error);
        }
        resp.getWriter().println(info);
        resp.getWriter().flush();
    }

    private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //得到分页参数
        String pagestr = req.getParameter("page");
        String pagesizestr = req.getParameter("pagesize");
        //转换为整数，提供默认值
        int page = StringUtils.isEmpty(pagestr) ? 1 : new Integer(pagestr);
        int pagesize = StringUtils.isEmpty(pagesizestr)? 5 : new Integer(pagesizestr);
        //得到查询对象
        String empSearch = req.getParameter("empSearch");
        if (!StringUtils.isEmpty(empSearch)) {
            //处理乱码
            String s = new String(empSearch.getBytes("ISO8859-1"), "UTF-8");
            //转换为学生对象
            EmpVo empVo = JSON.parseObject(s, EmpVo.class);
            //开始查询
            PageResult<Emp> search1 = empService.search(page, pagesize, empVo);
            //输出并刷新
            resp.getWriter().println(JSON.toJSONString(R.ok().data("search1",search1)));
            resp.getWriter().flush();
        }

    }

    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //得到分页参数
        String pagestr = req.getParameter("page");
        String pagesizestr = req.getParameter("pagesize");
        //转换为整数，提供默认值
        int page = StringUtils.isEmpty(pagestr) ? 1 : new Integer(pagestr);
        int pagesize = StringUtils.isEmpty(pagesizestr)? 5 : new Integer(pagesizestr);
        //开始分页
        PageResult<Emp> bypage = empService.findBypage(page, pagesize);

        //转换为JSON对象
        String s = JSON.toJSONString(bypage);
        //输出
        resp.getWriter().println(JSON.toJSONString(R.ok().data("bypage",bypage)));
        //刷新
        resp.getWriter().flush();

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //得到所有员工
        List<Emp> empList = empService.findAll();
        //输出员工
        R r = R.ok().data("list", empList);
        resp.getWriter().println(JSON.toJSONString(r));
        //刷新
        resp.getWriter().flush();
    }
}
